$(function() {
    // date类型设置默认选中当天日期
    var date = new Date();
    var day = ("0" + date.getDate()).slice(-2);
    // 取月份后两位（例如012=》12）
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var today = date.getFullYear() + "-" + (month) + "-" + (day);
    $('#code2').val(today);
    var year = date.getFullYear();
    var yy = year.toString().substr(2, 2);
    // number类型只能输入正整数
    $('#qtylimit').click(function() {
            var qty = $('#qtylimit').val();
            if (qty < 0) {
                qty = 0;
                $('#qtylimit').val(0);
            }
        })
        // 清除按钮
    $('.reset').click(function() {
        $('.clearInput').val('');
        $('.qtylimit').val(0);
        $('.locatorInput').val('请选择');
    })


    // 根据日期获取周数
    /**
     * 判断年份是否为润年
     *
     * @param {Number} year
     */
    function isLeapYear(year) {
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    /**
     * 获取某一年份的某一月份的天数
     *
     * @param {Number} year
     * @param {Number} month
     */
    function getMonthDays(year, month) {
        return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
    }
    /**
     * 获取某年的某天是第几周
     * @param {Number} y
     * @param {Number} m
     * @param {Number} d
     * @returns {Number}
     */
    function getWeekNumber(y, m, d) {
        var now = new Date(y, m - 1, d),
            year = now.getFullYear(),
            month = now.getMonth(),
            days = now.getDate();
        //那一天是那一年中的第多少天
        for (var i = 0; i < month; i++) {
            days += getMonthDays(year, i);
        }

        //那一年第一天是星期几
        var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
        //  console.log('yearFirstDay',yearFirstDay);
        var week = null;
        if (yearFirstDay == 1) {
            week = Math.ceil(days / yearFirstDay);
        } else {
            days -= (7 - yearFirstDay + 1);
            week = Math.ceil(days / 7) + 1;
        }
        if (week < 10) {
            week = '0' + week;
        }
        console.log('week', week)
        $('#code1').val(yy + week)
        return week;
    }
    var currentDate = $('#code2').val().split('-');
    getWeekNumber(currentDate[0], currentDate[1], currentDate[2])
    $('#code2').change(function() {
            var currentDate = $('#code2').val().split('-');
            getWeekNumber(currentDate[0], currentDate[1], currentDate[2])
        })
        //  getWeekNumber(currentDate[0],currentDate[1],currentDate[2])
})

// 获取site传过来的值
var paras = location.search; //search获得地址中的参数，内容为'?itemId=12'
var result = paras.match(/[^\?&]*=[^&]*/g); //match是字符串中符合的字段一个一个取出来，result中的值为['login=xx','table=admin']
paras = {}; //让paras变成没有内容的json对象
for (i in result) {
    var temp = result[i].split('='); //split()将一个字符串分解成一个数组,两次遍历result中的值分别为['itemId','xx']
    paras[temp[0]] = temp[1];
}
var theLine = paras.theLine; //根据参数名"lineName"，获取参数值
var theSegement = paras.theSegement;
$('.Terminal').val(decodeURI(theSegement));
$('.line').val(decodeURI(theLine));
// 返回site页
function goBack() {
    location.replace('home.html')
}

/**
 * 表单验证
 */
function checkBtn() {
    var pallet = document.getElementById('palletInput');
    var locator1 = document.getElementById('locatorInput1');
    var locator2 = document.getElementById('locatorInput2');
    console.log('palletInput', palletInput.value)
    if (pallet.value == '' || pallet.value == 'null' || pallet.value == 'undefined') {
        alert('栈板号不可为空！');
        pallet.focus();
        $('#palletInput').css('border', "1px solid rgb(51, 199, 150)")
        return false;
    }
    if (locator1.value == '请选择' || locator2.value == '请选择') {
        alert('请选择locator!');
        locator1.focus();
        return false;
    }
}

// 获取焦点时执行函数
$("input").focus(function() {
    $('#palletInput').addClass('focus')
})
$("input").blur(function() {
        $('#palletInput').removeClass('focus')
    })
    // 复选框点击事件  
var checkbox = document.getElementById('check');

function checkboxBtn() {
    $("body").attr("id", "disable");
    if (checkbox.checked) {} else {
        checkbox.checked  =  true;
        $('.bgdialog').removeClass('nodialog').addClass('isdialog');
    }
}
// 弹框确定事件
$(function() {
    $('.bgdialog').removeClass('isdialog').addClass('nodialog');
})

function dialogCertain() {
    $('.bgdialog').removeClass('isdialog').addClass('nodialog');
    $("body").removeAttr("id", "disable");
    checkbox.checked  = false;
}

function dialogCancle() {
    $('.bgdialog').removeClass('isdialog').addClass('nodialog');
    $("body").removeAttr("id", "disable");
    checkbox.checked  =  true;
}